package com.hmdp.config;


import java.util.concurrent.*;

public class sync {
    public static void main(String[] args) throws InterruptedException, ExecutionException {

        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                6,
                60,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1),//阻塞队列
                Executors.defaultThreadFactory(),//线程工厂
                new ThreadPoolExecutor.CallerRunsPolicy()//拒绝策略
        );
        Executors.newFixedThreadPool(10);
        Executors.newCachedThreadPool();
        Executors.newSingleThreadExecutor();
        Executors.newScheduledThreadPool(10);

        //异步有result
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> 1, threadPoolExecutor);
        System.out.println(future1.get());//1

        CompletableFuture.runAsync(() -> {////无result
        });
        CompletableFuture.supplyAsync(() -> 1, threadPoolExecutor)
                .whenComplete((res, exception) -> {//成功执行时回调，带返回值，失败res为null
                    System.out.println(res + " " + exception);//1 null
                }).exceptionally((e) -> {//出现异常返回0
                    return 0;
                });

        CompletableFuture.supplyAsync(() -> 1, threadPoolExecutor)
                .handle((r, e) -> {
                    if (r != null) {
                        return 1;
                    }
                    if (e != null) {
                        return 0;
                    }
                    return -1;
                });//成功，失败都处理

        //thenRun 无parameter 无result
        CompletableFuture<Void> serialization = CompletableFuture.supplyAsync(() -> 1, threadPoolExecutor)
                .thenRun(() -> {
                    System.out.println("开辟子");
                });
        //thenAcceptAsync 有parameter 无result
        CompletableFuture<Void> serialization2 = CompletableFuture.supplyAsync(() -> 1, threadPoolExecutor)
                .thenAcceptAsync((r1) -> {
                    System.out.println("r1:" + r1);
                }, threadPoolExecutor);
        //thenApplyAsync 有parameter 有result
        CompletableFuture<Integer> serialization3 = CompletableFuture.supplyAsync(() -> 1, threadPoolExecutor)
                .thenApplyAsync((r) -> r * 666, threadPoolExecutor);
        System.out.println(serialization3.get());

        //两任务组合-都要完成
        serialization.runAfterBothAsync(serialization, () -> System.out.println());//无
        serialization.thenAcceptBothAsync(serialization, (a, b) -> System.out.println());//有parameter
        serialization.thenCombineAsync(serialization, (a, b) -> 1);//有parameter有result
        //两任务组合-单个完成
        serialization.runAfterEitherAsync(serialization, () -> System.out.println());//无
        serialization.acceptEitherAsync(serialization, (a) -> System.out.println());//有parameter
        serialization.applyToEitherAsync(serialization, (a) -> 1);//有parameter有result
        //多任务组合
        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.allOf(serialization3, serialization, serialization2);
        voidCompletableFuture.join();//等待
        voidCompletableFuture.get();//等待
        CompletableFuture.anyOf(serialization3,serialization);
    }
}

